package helper.scxml.scxml2.t0.case2_traffic.zone.ts

import helper.base.BaseTypeHelper.toIntRange
import helper.base.LHMHelper.A2LHMExpand.add
import helper.base.debug.DebugHelper.DebuggerList.Companion.makeDebuggerList
import helper.base.debug.DebugRes.d_getCalString
import helper.base.debug.DebugRes.d_t_pT1Calculate_path
import helper.scxml.scxml2.t0.case2_traffic.zone.TrafficEnvHelper.StrategyObj.aStart
import helper.scxml.scxml2.t0.case2_traffic.zone.TrafficEnvHelper.StrategyObj.aTrainWait
import helper.scxml.scxml2.t0.case2_traffic.zone.TrafficEnvHelper.StrategyObj.bike
import helper.scxml.scxml2.t0.case2_traffic.zone.TrafficEnvHelper.StrategyObj.getSimpleT1EnvStrategy
import helper.scxml.scxml2.t0.case2_traffic.zone.TrafficEnvHelper.StrategyObj.train
import helper.scxml.scxml2.t0.case2_traffic.zone.TrafficEnvHelper.StrategyObj.trainWaitBack
import helper.scxml.scxml2.t0.case2_traffic.zone.TrafficEnvHelper.StrategyObj.trainWaitTrain
import helper.scxml.scxml2.t0.case2_traffic.zone.ts.T0Helper.TaskUnit.Companion.getTaskUnit
import helper.scxml.strategy.TotalStrategyHelper.KREWTuple.Companion.makeKREWTuple
import helper.scxml.strategy.TotalStrategyHelper.SNode.Companion.makeSNode
import helper.scxml.strategy.sNodeUnit.T2SNodeUnitHelper.ERT2SNodeUnit.Companion.makeL0ERT2SNodeUnit
import helper.scxml.strategy.sNodeUnit.T2SNodeUnitHelper.SubT2SNodeUnit.Companion.makeSubT2SNodeUnit
import helper.scxml.strategy.sNodeUnit.T2SNodeUnitHelper.T2SNodeUnit
import org.junit.Test

internal class T3_trainOne_byRetry_simple {
    fun getT3T2SNodeUnit(): T2SNodeUnit {
        val t2SNodeUnit = makeL0ERT2SNodeUnit(
            env = getSimpleT1EnvStrategy(),
            ren = makeSubT2SNodeUnit().apply {
                relationLHM.add(
                    null,
                    aStart,
                    makeSNode().also { aSNode ->
                        funLHM.add(aSNode) {
                            val enterS0Time = it.byNameFindC("r").getPointD()
                            if (enterS0Time <= 0) {
                                "r<=0"
                            } else {
                                "r>0"
                            }
                        }
                        arrayOf(
                            makeKREWTuple("r<=0", 1.toIntRange(), train, 1),
                            makeKREWTuple("r>0", 1.toIntRange(), bike, 1),
                        ).forEach {
                            addSNode(aSNode, it)
                        }
                    },
                )
                relationLHM.add(
                    null,
                    aTrainWait,
                    makeSNode().also { aSNode ->
                        funLHM.add(aSNode) {
                            val enterS0Time = it.byNameFindC("r").getPointD()
                            if (enterS0Time <= 0) {
                                "r<=0"
                            } else {
                                "r>0"
                            }
                        }
                        arrayOf(
                            makeKREWTuple("r<=0", 1.toIntRange(), trainWaitTrain, 1),
                            makeKREWTuple("r>0", 1.toIntRange(), trainWaitBack, 1),
                        ).forEach {
                            addSNode(aSNode, it)
                        }
                    },
                )
            },
        )
        println("t2SNodeUnit=${t2SNodeUnit.toL2String()}")
        return t2SNodeUnit
    }

    private val taskUnit = getTaskUnit(getT3T2SNodeUnit())

    @Test
    fun t1() {
        taskUnit.repeatRandomRun(
            1000,
            makeDebuggerList(
//                d_t_2023_0901_235859_,
            ),
        )
    }

    @Test
    fun t3() {
        taskUnit.t1Cal(
            makeDebuggerList(
                d_t_pT1Calculate_path,
                d_getCalString,
            )
        )
        //result={t=41.95454545454553}
    }

    @Test
    fun t111() {
        val res = (41 * 10 + (8 + 43.5)) / 11.0
        println(res)
        //41.95454545454545
    }

    @Test
    fun xiugai(){
        val old="""
nowConfig=((s0),(t∈[0.0,2.0],r=0.0),())
    timeEventChoice=[(1,s0s5)],nowConfig=((s5),(t=1.0,r=0.0),()),theWeightOfSum=1.0
        timeEventChoice=[(5,s5s6)],nowConfig=((s6),(t=6.0,r=0.0),()),theWeightOfSum=0.9090909090909091
            timeEventChoice=[(35,s6s9)],nowConfig=((s9),(t=41.0,r=0.0),()),theWeightOfSum=1.0
        timeEventChoice=[(5,s5s7)],nowConfig=((s7),(t=6.0,r=1.0),()),theWeightOfSum=0.09090909090909091
            timeEventChoice=[(1,s7s8)],nowConfig=((s8),(t=7.0,r=1.0),()),theWeightOfSum=1.0
                timeEventChoice=[(0,s8s0)],nowConfig=((s0),(t=7.0,r=1.0),()),theWeightOfSum=1.0
                    timeEventChoice=[(1,s0s1)],nowConfig=((s1),(t=8.0,r=1.0),()),theWeightOfSum=1.0
                        timeEventChoice=[(42,s1s9)],nowConfig=((s9),(t=50.0,r=1.0),()),theWeightOfSum=0.25
                        timeEventChoice=[(43,s1s9)],nowConfig=((s9),(t=51.0,r=1.0),()),theWeightOfSum=0.25
                        timeEventChoice=[(44,s1s9)],nowConfig=((s9),(t=52.0,r=1.0),()),theWeightOfSum=0.25
                        timeEventChoice=[(45,s1s9)],nowConfig=((s9),(t=53.0,r=1.0),()),theWeightOfSum=0.25
        """.trimIndent()

        var res = old

        (2..9).reversed().map {
            res = res.replace("s${it}","s${it+1}",)
        }
        println(res)
    }
}